package logic

import (
	"context"
	"srv/model/mysql"

	"srv/internal/svc"
	"srv/sesame"

	"github.com/zeromicro/go-zero/core/logx"
)

type DetailGoodsLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewDetailGoodsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DetailGoodsLogic {
	return &DetailGoodsLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *DetailGoodsLogic) DetailGoods(in *sesame.DetailGoodsRequest) (*sesame.DetailGoodsResponse, error) {
	g := mysql.Goods{}
	goods, err := g.DetailGoods(int(in.GoodsId))
	if err != nil {
		logx.Error("获取商品详情失败:", err)
		return nil, err
	}
	var goodsList []*sesame.GoodsItem
	for _, v := range goods {
		goodsList = append(goodsList, &sesame.GoodsItem{
			GoodsId:    int64(v.ID),
			GoodsTitle: v.GoodsTitle,
			GoodsCover: v.GoodsCover,
			GoodsPrice: float32(v.GoodsPrice),
		})
	}
	return &sesame.DetailGoodsResponse{
		GoodsDetail: goodsList,
	}, nil
}
